package mz.matafe.bolao

import org.codehaus.groovy.grails.plugins.springsecurity.Secured
import mz.matafe.bolao.Role
import mz.matafe.bolao.exception.*

/**
 * Role Controller.
 *
 * <p>Handle the Roles
 *
 * @since 0.0.1
 * @author <a href="mailto:matafe@gmail.com">Maurício T. Ferraz</a>
 */
class RoleController extends BolaoController{

    // the delete, save and update actions only accept POST requests
    static Map allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']

    def authenticateService

    def roleService

    @Secured(['ROLE_ADMIN'])
    def index = {
        redirect action: list, params: params
    }

    /** Display the list role page.  */
    @Secured(['ROLE_ADMIN'])
    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [roles: Role.list(params),  roleTotal: Role.count()]
    }

        /** Display the create new role page. */
    @Secured(['ROLE_ADMIN'])
    def create = {
        render "${message(code: 'action.unavailable')}"
        /*
        def role = new Role(params)
        [role: role]
        */
    }

    /** Save a new role. */
    @Secured(['ROLE_ADMIN'])
    def save = {
        render "${message(code: 'action.unavailable')}"
        /*
        def role = new Role()
        role.properties = params
        if (!role.hasErrors() && role.save()) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'role.label', default: 'Role'), group.id])}"
            redirect action: show, id: role.id
        }
        else {
            render view: 'create', model: [role: role]
        }
        */
    }

    /** Display the show role page. */
    @Secured(['ROLE_ADMIN'])
    def show = {
        def role = Role.get(params.id)
        if (!role) {
            flash.message = message(code:"role.not.found", args:["${params.id}"])
            redirect action: list
            return
        }
        [role: role]
    }

    /** Delete a role. */
    @Secured(['ROLE_ADMIN'])
    def delete = {
        render "${message(code: 'action.unavailable')}"
        /*
        def role = Role.get(params.id)
        if (!role) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'role.label', default: 'Role'), params.id])}"
            redirect action: list
            return
        }
        if (role.code == 'ROLE_ADMIN'){
            flash.message = message(code:"role.cannot.be.deleted", args:["${role.code}"])
            redirect action: list
            return
        }
        //authenticateService.deleteRole(role)
        try{
            roleService.delete(role.id)
            flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'role.label', default: 'Role'), params.code])}"
            redirect action: list
        }catch(ServiceException se){
            flash.message = message(code:se.messageKey, args:se.args)
            redirect(action:show,id:params.id)
        }
        */
    }

    /** Display the edit role page. */
    @Secured(['ROLE_ADMIN'])
    def edit = {
        def role = Role.get(params.id)
        if (!role) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'role.label', default: 'Role'), params.id])}"
            redirect action: list
            return
        }
        [role: role]
    }

    /** Role update action. */
    @Secured(['ROLE_ADMIN'])
    def update = {
        def role = Role.get(params.id)
        if (!role) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'role.label', default: 'Role'), params.id])}"
            redirect action: edit, id: params.id
            return
        }

        long version = params.version.toLong()
        if (role.version > version) {
            role.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'role.label', default: 'Role')] as Object[], "Another user has updated this Role while you were editing")
            render view: 'edit', model: [role: role]
            return
        }
        if (authenticateService.updateRole(role, params)) {
            //authenticateService.clearCachedRequestmaps()
            flash.message = "${message(code: 'default.updated.message', args: [message(code: 'role.label', default: 'Role'), role.code])}"
            redirect action: show, id: role.id
        }
        else {
            render view: 'edit', model: [role: role]
        }
    }
    
}
